<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 个人中心
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-04-30 09:50:32 +0800 (三, 2014-04-30) $
 * @version : $Id: center.php 89 2014-04-30 01:50:32Z spike $
 * @rev     : $Revision: 89 $
 */
class Center extends CI_Controller 
{  
    private static $_admin_id;

    public function __construct()
    {
        parent::__construct();
        if( ! self::$_admin_id = login_info('admin_id') )
        {
            show_error("请先登录后再访问该页面。");
        }
    }

    /**
     * [个人中心]
     */
    public function profile()
    {
        if($this->input->is_post_request())
        {
            $params             = $this->input->post();
            $params['admin_id'] = self::$_admin_id;

            temp('admin_edit', $params);

            $this->load->model('Rbac_model');
            if( FALSE === $this->Rbac_model->edit_admin($params) )
            {
                show_error($this->Rbac_model->get_error());
            }
            temp('admin_edit', NULL);

            show_message("编辑用户成功！", array(
                '返回个人中心' => DOMAIN."center/profile",
            ));
        }
        else
        {
            $this->load->model('Rbac_model');
            if( ! $admin = $this->Rbac_model->get_admin(self::$_admin_id, TRUE, TRUE, TRUE))
            {
                show_error($this->Rbac_model->get_error());
            }

            // 获取所有资源类型
            $role_resource_group = $this->config->item('role_resource_group');

            // 获取所有资源列表
            $this->load->dao('Role_resource_dao');
            $role_resource = $this->Role_resource_dao->get(
                array('is_delete' => UNDELETED),
                array('resource_id','name')
            );

            // 整理角色资源
            if( is_array($admin['privs']) )
            {
                $temp = array();
                foreach ($admin['privs'] as $ap) 
                {
                    foreach ($role_resource_group as $rg => $rgv) 
                    {
                        $col = strtolower($rg)."_priv";
                        if( $ap[$col] == ROLE_PRIV_ON )
                            $temp[] = "{$ap['resource_id']}-{$rg}";
                    }
                }
                $admin['privs'] = $temp;
            }

            $data = array(
                'admin'               => $admin,
                'temp'                => temp('admin_edit'),
                'role_resource_group' => $role_resource_group,
                'role_resource'       => $role_resource,
                'admin_gender'        => $this->config->item('admin_gender'),
                'css'                 => array(JS_DOMAIN.'dropzone/css/dropzone.css'),
                'js'                  => array('dropzone/dropzone.js'),
                'show_title'          => FALSE,
            );

            $this->load->view('home/header.tpl', $data);
            $this->load->view('center/profile.tpl');
            $this->load->view('home/footer.tpl');
        }

    }

    /**
     * [上传用户头像]
     * @return [json]
     */
    public function upload_avatar()
    {
        $dir = DATA_PATH."admin/".self::$_admin_id."/";
        if ( ! is_dir($dir)) 
        {
            mkdir($dir, DIR_WRITE_MODE);         
        };

        $config = array(
            'upload_path'   => $dir,
            'file_name'     => "avatar_upload.png",
            'allowed_types' => 'gif|jpg|png|jpeg',
            'max_size'      => 1024,
            'overwrite'     => TRUE,
        );
  
        $this->load->library('upload', $config);
 
        if ( ! $this->upload->do_upload('avatar_upload'))
        {
            set_status_header(500);
            send_header('json');
            echo '{ "error": "'.$this->upload->display_errors().'"}';
            exit;
        }

        $config = array(
            'image_library' => 'gd2',
            'source_image'  => $dir.'avatar_upload.png',
            'create_thumb'  => TRUE,
            'width'         => 70,
            'height'        => 70,
            'maintain_ratio'=> TRUE,
            'quality'       => 100,
        );

        $this->load->library('image_lib', $config);
        $this->image_lib->resize();

        $this->load->model('Rbac_model');
        $this->Rbac_model->update_avatar_version(self::$_admin_id);

        send_header('json');
        echo '{ "ok": "修改头像已成功"}';
        exit;
    }

    /**
     * [修改密码]
     */
    public function change_password()
    {
        if( ! $admin_id = login_info('admin_id') )
        {
            show_error("请先登录后再访问该页面。");
        }

        $old_password         = $this->input->post('old_password');
        $new_password         = $this->input->post('new_password');
        $new_password_confirm = $this->input->post('new_password_confirm');
        if($new_password !== $new_password_confirm)
        {
            show_error("修改密码失败：两次输入的新密码不一致");
        }


        $this->load->model('Rbac_model');
        if( FALSE === $this->Rbac_model->change_admin_password(self::$_admin_id, $old_password, $new_password) )
        {
            show_error($this->Rbac_model->get_error());
        }

        show_message("修改密码成功！", array(
            '重新登录' => DOMAIN."home/logout",
        ));
    }
}

/* End of file Center.php */